package com.wangx1n.boobey.util;

import com.wangx1n.boobey.dao.EnWordsMapper;
import com.wangx1n.boobey.entity.pk.EnWords;
import com.wangx1n.boobey.entity.pk.Problem;
import com.wangx1n.boobey.service.EnWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Random;

/**
 * @author wangxin
 * @create 2020-06-14 20:41
 */
@Component
public class WordUtil {
    private static final Integer SCORE = 2;
    @Autowired
    EnWordService enWordService;
    @Autowired
    EnWordsMapper enWordsMapper ;

    public static WordUtil wordUtill;

    @PostConstruct
    public void init() {
        wordUtill = this;
    }

    /**
     * 生成题目
     * @param dictionary 题库编号
     * @return
     */
//    public static   Problem makeProblem(EnWordsMapper enWordsMapper, int i){
//        System.out.println(enWordsMapper.selectByPrimaryKey(1));
//        return null;
//    }

    //utils工具类中使用service和mapper接口的方法例子，用"testUtils.xxx.方法" 就可以了

    public static  Problem makeProblem(int dictionary){
        //实例化题目对象
        Problem problem = new Problem();
        //实例化单词对象
        EnWords enWords = new EnWords();
        Random random = new Random();
        //生成问题序号
         int subjectIndex =  random.nextInt(103975) + 1;
         //生成随机正确选项
         int answerIndex = random.nextInt(4) + 1;
         //查找正确的单词
         enWords = wordUtill.enWordService.selectWordById(subjectIndex);
         //设置得分
        problem.setScore(SCORE);

         //设置题目
         problem.setSubject(enWords.getWord());
         //设置答案
         problem.setAnswer(answerIndex);
         switch (answerIndex){
             case 1:
                problem.setChoiceA(enWords.getTranslation());
                 for(int i=0;i<3;i++){
                     problem.setChoiceB(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                     problem.setChoiceC(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                     problem.setChoiceD(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                 }
                break;
             case 2:
                 problem.setChoiceB(enWords.getTranslation());
                 for(int i=0;i<3;i++){
                     problem.setChoiceA(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                     problem.setChoiceC(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                     problem.setChoiceD(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                 }
                 break;
             case 3:
                 problem.setChoiceC(enWords.getTranslation());
                 for(int i=0;i<3;i++){
                     problem.setChoiceB(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                     problem.setChoiceA(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                     problem.setChoiceD(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                 }
                 break;
             case 4:
                 problem.setChoiceD(enWords.getTranslation());
                 for(int i=0;i<3;i++){
                     problem.setChoiceB(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                     problem.setChoiceC(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                     problem.setChoiceA(wordUtill.enWordService.selectWordById(random.nextInt(103975) + 1).getTranslation());
                 }
                 break;
         }
         return problem;
    }
}
